package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.subview

import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONObject

class HotlineGhanaDialog : DialogFragment() {

    private lateinit var adapter: HotlineGhanaDialogAdapter

    private lateinit var list: RecyclerView
    private lateinit var close: ImageView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_ghana_hotline_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        list = view.findViewById(R.id.list)
        close = view.findViewById(R.id.close)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        // update list
        adapter.notifyDataSetChanged()
    }

    private fun initializeView() {

        adapter = HotlineGhanaDialogAdapter(ProjectApplication.mHotlines)
        list.layoutManager = LinearLayoutManager(context)
        list.adapter = adapter

        close.setOnClickListener {
            dismiss()
        }
    }
}

/**
 * adapter
 */
class HotlineGhanaDialogAdapter(private val dataList: List<JSONObject>) :
    RecyclerView.Adapter<HotlineGhanaDialogAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val title: TextView = view.findViewById(R.id.title)
        val action: TextView = view.findViewById(R.id.action)
        val tips: TextView = view.findViewById(R.id.tips)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_ghana_hotline_dialog_adapter, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item: JSONObject = dataList[position]

        val isMobile = item.optInt("type", 0)
        if (isMobile == 0) {
            holder.action.text = "Dial"
            holder.tips.text = "Customer service hotline ${position + 1}"
        } else {
            holder.action.text = "Copy"
            holder.tips.text = "Customer service Email ${position + 1}"
        }

        val contact = item.optString("title")
        holder.title.text = contact

        holder.action.setOnClickListener {
            if (isMobile == 0) {
                val intent = Intent(Intent.ACTION_DIAL).apply {
                    data = Uri.parse("tel:${Uri.encode(contact)}")
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
                }
                ProjectApplication.instance.startActivity(intent)

            } else {
                val clipboardManager =
                    ProjectApplication.instance.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                clipboardManager.setPrimaryClip(ClipData.newPlainText("Label", contact))

                Toast.makeText(
                    ProjectApplication.instance,
                    "Copy Success",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}
